<template>
  <div class="my-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav" title="登录">
      <template #left>
        <van-icon name="cross" @click="$router.back()" />
      </template>
    </van-nav-bar>
    <!-- 未登录 -->
    <div class="header not-login" v-if="!user.token">
      <div class="login-btn">
        <img class="mobile-img" src="~@/assets/mobile.png" @click="$router.push('/login')" />
        <span class="text">登录/注册</span>
      </div>
    </div>
    <!-- /未登录 -->
    <!-- 已登录 -->
    <div class="userInfo header" v-else>
      <!-- 基本信息 -->
      <div class="base">
        <div class="left">
          <van-image fit="cover" :src="userInfo.photo" class="avatar" round />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button to="/user/profile" type="default" size="mini" round>编辑资料</van-button>
        </div>
      </div>
      <!-- 粉丝、关注 -->
      <div class="data">
        <div class="data-item">
          <span>{{ userInfo.art_count }}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{ userInfo.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{ userInfo.fans_count }}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>4</span>
          <span>获赞</span>
        </div>
      </div>
      <!-- /已登录 -->

      <!--        <div>234</div>-->
      <van-grid clickable :column-num="2" class="nav-grid">
        <van-grid-item to="/">
          <template #icon>
            <i class="toutiao toutiao-shoucang"></i>
          </template>

          <template #text>
            <span class="text">收藏</span>
          </template>
        </van-grid-item>
        <van-grid-item url="/vant/mobile.html">
          <template #icon>
            <i class="toutiao toutiao-lishi"></i>
          </template>
          <template #text>
            <span class="text">历史</span>
          </template>
        </van-grid-item>
      </van-grid>
    </div>
    <!-- /已登录 -->
    <!--    单元格导航-->
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />
    <van-cell @click="logOut" v-if="user.token" class="logout-cell" title="退出登录" center />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'MyPage',
  data() {
    return {
      userInfo: '',
    }
  },
  async created() {
    if (!this.user.token) {
      return
    }
    this.$toast.loading({
      message: '登录中',
      duration: 0, //显示加载中
    })

    try {
      const res = await getUserInfo()

      this.userInfo = res.data.data
      console.log(this.userInfo)
      this.$toast.success('登录成功')
    } catch (error) {
      this.$toast.fail('获取失败')
    }
  },
  computed: {
    ...mapState(['user']),
  },
  methods: {
    logOut() {
      this.$dialog
        .confirm({ title: '温馨提示', message: '是否确认退出登录' })
        .then(() => {
          this.$store.commit('setToken', {})
        })
        .catch(() => {
          console.log('取消')
        })
    },
  },
}
</script>

<style scoped lang="less">
.my-container {
  > .header {
    background: url('~@/assets/banner.png') no-repeat;
    background-size: cover;
    &.not-login {
      height: 361px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .mobile-img {
      width: 132px;
      height: 132px;
    }

    .login-btn {
      display: flex;
      flex-direction: column;
      align-items: center;

      .text {
        font-size: 30px;
        color: #fff;
        margin-top: 15px;
      }
    }
  }
}

.userInfo {
  background: red;
  .base {
    height: 231px;
    padding: 76px 32px 23px;
    //background: #ccc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;

    .name {
      color: #fff;
    }
    .left {
      display: flex;
      align-items: center;
      .avatar {
        width: 132px;
        height: 132px;
        border: 2px solid #fff;
      }
      .name {
        font-size: 29px;
        margin-left: 22px;
      }
    }
  }
}

.data {
  display: flex;
  justify-content: space-around;
  .data-item {
    height: 130px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: #fff;
    span:nth-child(1) {
      font-size: 36px;
      margin-bottom: 15px;
    }
    span:nth-child(2) {
      font-size: 23px;
    }
  }
}

.nav-grid {
  background: #fff;
  .toutiao {
    font-size: 45px;
  }
  .toutiao-shoucang {
    color: #eb5253;
  }
  .toutiao-lishi {
    color: #ff9d1d;
  }
  .text {
    color: #000;
    margin-top: 8px;
    font-size: 28px;
  }
}

.logout-cell {
  height: 100px;
  text-align: center;
  color: #eb5253;
  margin: 10px 0;
  font-size: 30px;
}
</style>
